<?php
require_once(dirname(__FILE__).'/Abstract.php');
class Patient extends AbstractDoctorController 
{


    public function indexAction()
    {
    	$this->listingAction();
    }

    public function listingAction()
    {
    	
        $d = array();
        $d['title'] = $this->l('List of patients');
        $this->load->model('ORM/User/UserEntityORM');
        $d['users'] = $this->UserEntityORM->getAllByIdRole($this->getConfig('idRolePatient'));//();

        $d['title_for_layout'] = $this->l('Patients list.');
        $d['menu'] = 'patients';
        $d['page_name'] = 'patients_list';
        
        $this->load->view('Templates/Layouts/Doctor/header', $d);
        $this->load->view('Doctor/Patient/Listing', $d);
        $this->load->view('Templates/Layouts/Doctor/footer', $d);

    }

    public function activatePatientAction($id_user = NULL)
    {
        $this->load->model('ORM/User/UserEntityORM');
        $record = array(
                    'id_user'   => $id_user, 
                    'activated' => 2
                );
        if( $this->UserEntityORM->save($record) ){
            $this->session->set_flashdata('successMessage', $this->l('The patient have been activated!'));
        } else {
            $this->session->set_flashdata('errorMessage', $this->l('Error, retry after')); 
        }
        
        redirect('Doctor/Patient/listing');
        die();
    }

    public function viewPatientAction($id_user = NULL)
    {

        $d = array();
        $d['title'] = $this->l('Patient profile');

        $this->load->model('ORM/User/UserEntityORM');
        $this->load->model('ORM/User/UserPatientORM');
        $d['user'] = $this->UserEntityORM->getByIdUser($id_user);
        $d['patient'] = $this->UserPatientORM->getByIdUser($id_user);
        
        $d['title_for_layout'] = $this->l('Patient profile.');
        $d['menu'] = 'patients';
        $d['page_name'] = 'patients_list';   
        
        $this->load->view('Templates/Layouts/Doctor/header', $d);
        $this->load->view('Doctor/Patient/ViewPatient', $d);
        $this->load->view('Templates/Layouts/Doctor/footer', $d);


    }
    
    public function viewLogsAction($id_consultation = NULL)
    {
        $d = array();
        $d['title'] = $this->l('Consultation logs');
        
        $this->load->model('ORM/Consultation/ConsultationLogORM');
        $this->load->model('ORM/Consultation/ConsultationORM');
        $this->load->model('ORM/User/UserEntityORM');
        $this->load->model('ORM/User/UserPatientORM');
        $log_table = $this->ConsultationLogORM->getTableName();
        $log_table_primary = $this->ConsultationLogORM->getPrimaryKey();
        $cons_table = $this->ConsultationORM->getTableName();        
        $cons_table_primary = $this->ConsultationORM->getPrimaryKey();
        $users_table = $this->UserEntityORM->getTableName();        
        $users_table_primary = $this->UserEntityORM->getPrimaryKey();
        $patients_table = $this->UserPatientORM->getTableName();        
        $patients_table_primary = $this->UserPatientORM->getPrimaryKey();
        
        $this->db->select('logs.*, cons.*, users.*');
        $this->db->from($log_table.' as logs');
        $this->db->join($cons_table.' as cons', 'cons.'.$cons_table_primary.'='.'logs.'.$cons_table_primary, 'left');
        $this->db->join($users_table.' as users', 'cons.'.$patients_table_primary.'='.'users.'.$users_table_primary, 'left');
        $query = $this->db->get();
        $d['logs'] = $query->result();
        
        $d['title_for_layout'] = $this->l('Consultation logs.');
        $d['menu'] = 'consultations';
        $d['page_name'] = 'consultaion_logs';  
        
        $this->load->view('Templates/Layouts/Doctor/header', $d);
        $this->load->view('Doctor/Patient/ViewLogs', $d);
        $this->load->view('Templates/Layouts/Doctor/footer', $d);
    }

    public function connectedPatientsAction(){

        $d = array();
        $d['title'] = $this->l('List of connected patients');
    
        $this->load->model('ORM/User/UserEntityORM');
        $d['connected_patients'] = $this->UserEntityORM->getAllByIdRoleAndIsConnected($this->getConfig('idRolePatient'), '1');
        
        $d['title_for_layout'] = $this->l('Connected patients.');
        $d['menu'] = 'patients';
        $d['page_name'] = 'connected_patients';
        
        $this->load->view('Templates/Layouts/Doctor/header', $d);
        $this->load->view('Doctor/Patient/ConnectedPatients', $d);
        $this->load->view('Templates/Layouts/Doctor/footer', $d);

   
    }

    public function exportListOfPatientsAction()
    {
        
    }
}

